﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering_Rules;
using Catering.View_Operaction;
using System.Data;
using System.Collections;
using System.Text;

namespace Catering.Catering_Page.Mes
{
    public partial class NewMes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadPage();
                TreeView1.Attributes.Add("onclick", "OnTreeNodeChecked()");
            }
        }

        private void loadPage()
        {
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            Bind_Data_to_Controls.Bing_Personnel_To_TreeView(TreeView1, pid, true);
        }
        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            string cText = txtContent.Text;
            if (Session["Receivers"] == null)
            {
                Response.Write(Bind_Data_to_Controls.MessageAlert("请选择信息接受人！"));
                return;
            }
            ArrayList receivers = Session["Receivers"] as ArrayList;
            string[] receiverIDs = new string[receivers.Count];
            for (int i = 0; i < receivers.Count; i++)
            {
                receiverIDs[i] = receivers[i].ToString();
            }
            if (receiverIDs != null && receiverIDs.Count() > 0)
            {
                Catering_Model.SMS_MsgInfo model = new Catering_Model.SMS_MsgInfo();
                model.CText = txtContent.Text;
                model.MesType = 0;//表示内部短信
                model.Sender = new Catering_Model.user();
                model.Sender.id = Convert.ToInt32(Session["UserID"]);
                Catering_Rules.SMS_MsgBLL smsRules = new SMS_MsgBLL();
                if (smsRules.addsms(model, receiverIDs))
                {
                    Response.Write(Bind_Data_to_Controls.MessageAlert("信息发送成功，即将跳转页面。"));
                    Response.Redirect("SMSHall.aspx");
                }
            }
        }
        protected void btnSelectRecevie_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Name"));
            ArrayList users = new ArrayList();
            foreach (TreeNode root in TreeView1.Nodes)
            {
                foreach (TreeNode Parent in root.ChildNodes)
                {
                    if (Parent.Checked)
                    {
                        DataRow dr = dt.NewRow();
                        dr["Name"] = Parent.Text;
                        dt.Rows.Add(dr);
                        List<Catering_Model.E_Personnel> listPerson = new Catering_Rules.T_Personnel().GetModelList(" ClassID=" + Parent.Value + " AND (PID=11 or pid=13 or ClassID=50) ");
                        foreach (Catering_Model.E_Personnel item in listPerson)
                        {
                            users.Add(item.id);
                        }
                    }
                    //foreach (TreeNode Child in Parent.ChildNodes)
                    //{
                    //    if (Child.Checked)
                    //    {
                    //        DataRow dr = dt.NewRow();
                    //        dr["Name"] = Child.Text;
                    //        dt.Rows.Add(dr);
                    //        users.Add(Child.Value);
                    //    }
                    //}
                }
            }
            Session["Receivers"] = users;
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtSearch.Text))
            {
                string searchText = txtSearch.Text;
                foreach (TreeNode root in TreeView1.Nodes)
                {
                    foreach (TreeNode Parent in root.ChildNodes)
                    {
                        foreach (TreeNode Child in Parent.ChildNodes)
                        {
                            if (Child.Text.Contains(searchText))
                            {
                                Child.Checked = true;
                                Child.Expand();
                                Child.Parent.Expand();
                                Child.Parent.Parent.Expand();
                                break;
                            }
                        }
                    }
                }
            }
        }

        #region 目前无效的代码
        //private void RepeaterDataBind(DataTable dt, Repeater rpt)
        //{
        //    PagedDataSource objPage = new PagedDataSource();
        //    //设置数据源 DefaultView：获取可能包含筛选视图或游标位置表的自定义视图
        //    objPage.DataSource = dt.DefaultView;
        //    //允许分页
        //    objPage.AllowPaging = true;
        //    //设置每页个数
        //    objPage.PageSize = 12;
        //    //定义变量保存当前页的索引值

        //    int CurPage;
        //    //判断是否具有页面跳转的请求
        //    if (Request.QueryString["Page"] != null)
        //    {
        //        CurPage = Convert.ToInt32(Request.QueryString["Page"]);
        //    }
        //    else
        //    {
        //        CurPage = 1;
        //    }
        //    //设置当前页的索引
        //    objPage.CurrentPageIndex = CurPage - 1;
        //    //显示状态信息
        //    lblCurPage.Text = "第" + CurPage.ToString() + "/" + objPage.PageCount + "页";
        //    //如果当前页面不是首页
        //    if (!objPage.IsFirstPage)
        //    {
        //        //定义“上一页”超级链接的URL为：当前执行页面的虚拟路径，并传递下一页面的索引值 
        //        InkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);

        //    }
        //    InkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + (objPage.PageCount);
        //    InkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
        //    //如果当前页面不是最后一页
        //    if (!objPage.IsLastPage)
        //    {
        //        //定义“下一页”超级链接的URL为：当前执行页面的虚拟路径，并传递下一页面的索引值 
        //        InkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
        //    }
        //    //将PagedDataSource作为数据源
        //    rpt.DataSource = objPage;

        //    //进行数据绑定
        //    rpt.DataBind();
        //}
        protected void Button1_Click(object sender, EventArgs e)
        {
            //divmain.Style["display"] = "none";
            //divup.Style["display"] = "none";
            //ArrayList AList = new ArrayList();
            //ArrayList NList = new ArrayList();
            //foreach (RepeaterItem item in RptOutBox.Items)
            //{
            //    CheckBox cbo = item.FindControl("RPCheckBoxUserID") as CheckBox;
            //    if (cbo != null && cbo.Checked)
            //    {
            //        Label lblUserId = item.FindControl("RPlblUserId") as Label;
            //        Label lblName = item.FindControl("RPlblName") as Label;
            //        if (Session["receiver"] == null)
            //        {
            //            if (!string.IsNullOrEmpty(lblUserId.Text))
            //            {
            //                AList.Add(lblUserId.Text);
            //            }
            //            if (!string.IsNullOrEmpty(lblName.Text))
            //            {
            //                NList.Add(lblName.Text);
            //            }
            //        }
            //        else
            //        {
            //            string[] strNum = Session["receiver"] as string[];
            //            if (strNum.Where(p=>p.Equals(lblUserId.Text)).Count() > 0)
            //            {
            //                continue;
            //            }
            //            else
            //            {
            //                if (!string.IsNullOrEmpty(lblUserId.Text))
            //                {
            //                    AList.Add(lblUserId.Text);
            //                }
            //                if (!string.IsNullOrEmpty(lblName.Text))
            //                {
            //                    NList.Add(lblName.Text);
            //                }
            //            }
            //        }
            //    }
            //}
            //if (AList.Count > 0)
            //{
            //    string[] strNum = new string[AList.Count];
            //    for (int i = 0; i < AList.Count; i++)
            //    {
            //        strNum[i] = AList[i].ToString();
            //    }
            //    Session["receiver"] = strNum;
            //}
            //if (NList.Count > 0)
            //{
            //    for (int i = 0; i < NList.Count; i++)
            //    {
            //        txtReceiver.Text += NList[i] + ";";
            //    }
            //}
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            //divmain.Style["display"] = "block";
            //divup.Style["display"] = "block";

        }
        //protected void txtReceiver_TextChanged(object sender, EventArgs e)
        //{
        //    string[] str = txtReceiver.Text.Split(';');
        //    Unit u = new Unit(24 * Convert.ToDouble((Convert.ToDouble(str.Count()) / 6.5)));
        //    txtReceiver.Height = u;
        //}
        #endregion
    }
}